import 'package:bitcoin_mine/common/global.dart';
import 'package:bitcoin_mine/controllers/contract_service_controller.dart';
import 'package:bitcoin_mine/widgets/restore_purchases_section.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import '../widgets/header_section.dart';
import '../widgets/subscription_tiers.dart';
import '../widgets/subscription_plans.dart';
import '../widgets/beginner_mining_section.dart';
import '../controllers/home_controller.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final homeController = Get.put(HomeController());
    final contractServiceController = Get.find<ContractServiceController>();
    return Stack(
      children: [
        Image.asset('assets/images/pic_home_bg.png', fit: BoxFit.cover),
        SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(height: 50.h),
              // 头部区域
              const HeaderSection(),

              // Image.asset(
              //   'assets/images/pic_home_top.png',
              //   width: 364.w,
              //   height: 273.h,
              //   fit: BoxFit.cover,
              // ),
              Stack(
                children: [
                  Padding(
                    padding: EdgeInsets.only(top: 20.h),
                    child: Center(
                      child: Lottie.asset(
                        'assets/anim/calc/data.json',
                        width: 364.w,
                        height: 273.h,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 15.w),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          width: 123.w,
                          height: 36.h,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage(
                                "assets/images/bg_home_top_speed.png",
                              ),
                            ),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Image.asset(
                                "assets/images/icon_speed.png",
                                width: 32.w,
                                height: 32.h,
                              ),
                              SizedBox(width: 4.w),
                              Obx(
                                () => Text(
                                  '${Global.totalHashrate.value.toString()} Gh/s',
                                  style: TextStyle(
                                    fontSize: 12.sp,
                                    color: Color(0xFF05FFFD),
                                    fontWeight: FontWeight.w800,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          width: 123.w,
                          height: 36.h,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage(
                                "assets/images/bg_home_top_speed.png",
                              ),
                            ),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Image.asset(
                                "assets/images/icon_contract.png",
                                width: 32.w,
                                height: 32.h,
                              ),
                              SizedBox(width: 4.w),
                              Obx(
                                () => Text(
                                  "${Global.contracts.length} Copy",
                                  style: TextStyle(
                                    fontSize: 12.sp,
                                    color: Color(0xFF05FFFD),
                                    fontWeight: FontWeight.w800,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              // // 挖矿状态卡片
              // const MiningStatusCard(),
              SizedBox(height: 10.h),
              GestureDetector(
                onTap: () {
                  contractServiceController.watchAds();
                },
                child: Stack(
                  children: [
                    Align(
                      alignment: Alignment.center,
                      child: Image.asset(
                        'assets/images/pic_watch_ads.png',
                        width: 374.w,
                        fit: BoxFit.cover,
                      ),
                    ),
                    Obx(() {
                      if (contractServiceController.adLoading.value) {
                        return Container(
                          width: 374.w,
                          height: 75.h,
                          color: Colors.black.withOpacity(0.5),
                        );
                      }
                      return Container();
                    }),
                  ],
                ),
              ),
              Obx(() {
                if (Global.mineInfo.value?.free_contract == null) {
                  return Column(
                    children: [
                      SizedBox(height: 15.h),
                      GestureDetector(
                        onTap: () {
                          contractServiceController.receiveFreeContract();
                        },
                        child: Image.asset(
                          'assets/images/pic_free_contract_inactive.png',
                          width: 374.w,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ],
                  );
                } else {
                  if (Global.mineInfo.value?.free_contract?.status == 1) {
                    return Column(
                      children: [
                        SizedBox(height: 15.h),
                        Image.asset(
                          'assets/images/pic_free_contract_active.png',
                          width: 374.w,
                          fit: BoxFit.cover,
                        ),
                      ],
                    );
                  } else {
                    return Container();
                  }
                }
              }),

              // 订阅层级选择
              const SubscriptionTiers(),

              // 订阅计划选择
              const SubscriptionPlans(),

              // 恢复购买说明
              const RestorePurchasesSection(),

              // 底部间距
              SizedBox(height: 60.h),
            ],
          ),
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child: const BeginnerMiningSection(),
        ),
      ],
    );
  }
}
